<?
class GetPhotoController{
	
	private $zdjecie;
	private $szerokosc;
	private $wysokosc;
	private $orginal_width;
	private $orginal_height;
	private $thumb;
	private $error = false;
	private $orginal;
	private $kompresja;
	
	function __construct(){
		$par = explode(',',isset($_REQUEST['parameters'])? $_REQUEST['parameters']: "");
		$par[0] = '..'.$par[0];
		$this->zdjecie = $par[0];
		$this->szerokosc = $par[1];
		$this->wysokosc = $par[2];
		$this->kompresja = $par[3];
		$this->resize();
		$this->thumb = $this->zdjecie."_{$this->szerokosc}x{$this->wysokosc}.thumb";
		if($this->error){
			
		}else{
			if (is_file($this->thumb)){
				$this->pobiez_thumb();				
			}else{
				$this->generuj_thumb();
			}
		}	
	}

	
	private function resize(){
		if (is_file($this->zdjecie)){
			list($this->orginal_width, $this->orginal_height, $type, $attr ) = $this->orginal = getimagesize($this->zdjecie);
			$max_height = $this->wysokosc;
			$max_width = $this->szerokosc;

		
			$x_ratio = $max_width / $this->orginal_width;
			$y_ratio = $max_height / $this->orginal_height;
		
			if (($this->orginal_width <= $max_width) && ($this->orginal_height <= $max_height) ) {
				$this->szerokosc = $this->orginal_width;
				$this->wysokosc = $this->orginal_height;
			} else if (($x_ratio * $this->orginal_height) < $max_height) {
				$this->wysokosc = ceil($x_ratio * $this->orginal_height);
				$this->szerokosc = $max_width;
			} else {
				$this->szerokosc = ceil($y_ratio * $this->orginal_width);
				$this->wysokosc = $max_height;
			}
		}else{
			$this->error = true;
		}	
	}
	
	private function generuj_thumb(){		
		$miniaturka = imagecreatetruecolor($this->szerokosc, $this->wysokosc);
		header("Content-type: image/jpeg");	
		$orginal = $this->get_format();
		imagecopyresampled($miniaturka, $orginal, 0, 0, 0, 0, $this->szerokosc, $this->wysokosc, $this->orginal_width, $this->orginal_height);
		if(!imagejpeg($miniaturka, $this->thumb, $this->kompresja));		
		imagestring($miniaturka, 1, $this->szerokosc, $this->wysokosc, ".", imagecolorallocate(imagecreatetruecolor($this->szerokosc,$this->wysokosc),100,100,100));					
		if(!imagejpeg($miniaturka, null, $this->kompresja));
		@imagedestroy($orginal);
		@imagedestroy($miniaturka);
	}	
	
	private function pobiez_thumb(){	
		header("Content-type: image/jpeg");	
		$file = @fopen($this->thumb,"rb");
		if ($file) {
		  while(!feof($file)) {
		    print(fread($file, 1024*1));
		    flush();
		    if (connection_status()!=0) {
		      @fclose($file);
		      die();
		    }
		  }
		  @fclose($file);
		}		
	}

	private function get_format(){
		switch ($this->orginal['mime']){
			case 'image/bmp':
				$orginal = @imagecreatefromwbmp($this->zdjecie);
				break;
			case 'image/gif':
				$orginal = @imagecreatefromgif($this->zdjecie);	
				break;			
			case 'image/pjpeg':
				$orginal = @imagecreatefromjpeg($this->zdjecie);
				break;	
			case 'image/jpeg':
				$orginal = @imagecreatefromjpeg($this->zdjecie);
				break;		
			case 'image/png':
				$orginal = @imagecreatefromjpeg($this->zdjecie);
				break;	
			default:
				$orginal = false;							
		}
		return $orginal;		
	}
	
	private function blank_thumb(){
		$string = "brak: ".$this->zdjecie;
		$obrazek = imagecreate($this->szerokosc, $this->wysokosc);
		$bg = imagecolorallocate($obrazek, 0, 0, 0);
		ImageFill($obrazek, 0, 0, $bg); 
		$textcolor = imagecolorallocate($obrazek, 0, 0, 255);
		imagestring($obrazek, 3, ($this->szerokosc - (strlen($string)*9))/2, $this->wysokosc/2-7, $string, $textcolor);
		header("Content-type: image/png");
		imagepng($obrazek); 
		@imagedestroy($obrazek);	
	}
}	
?>